package com.review.Demo_9_单例模式;

import com.sun.org.apache.bcel.internal.classfile.InnerClass;


public class Singletion {
	
//	//饿汉方式
//	private Singletion() {}
//	
//	private static Singletion instance = new Singletion();
//	
//	public static Singletion getInstance() {
//		return instance;
//	} 
	
	//懒汉
//	private Singletion(){}
//
//	private static Singletion instance = null;
//	
//	public static Singletion getInstance() {
//		if (instance == null) {
//			instance = new Singletion();
//		}
//		return instance;
//	}
	
	//懒汉优化：锁+双重判断
//	private Singletion(){}
//	
//	private static Singletion instance = null;
//	
//	public static Singletion getInstance() {
//		if (instance == null) {
//			synchronized (Singletion.class) {
//				if (instance == null) {
//					instance = new Singletion();
//				}
//			}
//		}
//		return instance;
//	}
	
//	//静态内部类
//	private Singletion() {}
//	
//	
//	private static class 内部{
//		private static Singletion instance = new Singletion();
//	}
//	
//	public static Singletion getInstance() {
//		return 内部.instance;
//	}
	
	//静态代码块
	private Singletion(){}
	
	private static Singletion instance = null;
	
	static {
		instance = new Singletion();
		System.out.println("bb");
	}
	
	public static void print(){
		System.out.println("aaaa");
	}
	
	public static Singletion getInstance() {
		return instance;
	}
	
	//调用Singletion.getInstance()时，会把Singletion的符号变量放到常量池中，解析时符号引用--->直接引用，顺便完成类加载，执行其static的东西
}
